let range = {
  from: 1,
  to: 5,
  [Symbol.iterator]: function () {
    return {
      current: this.from,
      last: this.to,

      next() {
        if (this.current <= this.last) {
          return {
            done: false,
            value: this.current++,
          };
        } else {
          return { done: true };
        }
      },
    };
  },
};

for (const item of range) {
  console.log(item);
}

const str = "test";

// for (const item of str) {
//   console.log(item);
// }

// for (let char of "test") {
//   // 触发 4 次，每个字符一次
//   console.log(char); // t, then e, then s, then t
// }

const strIterator = str[Symbol.iterator]();
while (true) {
  let result = strIterator.next();
  if (result.done) break;
  console.log(result.value); // 一个接一个地输出字符
}

let arrayLike = {
  0: "Hello",
  1: "world",
  length: 2,
};

console.log(Array.from(arrayLike));
console.log(Array.from(arrayLike, (item) => item + "map"));

let join = { name: "john" };

let messages = [
  { text: "Hello", from: "John" },
  { text: "How goes?", from: "John" },
  { text: "See you soon", from: "Alice" },
];
const weakMap = new WeakMap([messages]);
console.log(weakMap);

function makeCouter() {
  let count = 0;

  return function () {
    return ++count;
  };
}

let counter = makeCouter();

function func() {
  console.log(x);
  var x = 2;
}

func();

const syntax = new SyntaxError("语法错误");

console.log(syntax.name, syntax.message, syntax.stack, syntax.cloumnNumber);
